use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(ArticleTranslation::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(ArticleTranslation::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(ArticleTranslation::ArticleId).uuid().not_null())
                    .col(ColumnDef::new(ArticleTranslation::LanguageCode).string().not_null())
                    .col(ColumnDef::new(ArticleTranslation::Title).string().not_null())
                    .col(ColumnDef::new(ArticleTranslation::Content).text().not_null())
                    .col(ColumnDef::new(ArticleTranslation::Excerpt).string())
                    .col(ColumnDef::new(ArticleTranslation::MetaTitle).string())
                    .col(ColumnDef::new(ArticleTranslation::MetaDescription).string())
                    .col(ColumnDef::new(ArticleTranslation::CreatedAt).timestamp_with_time_zone().not_null())
                    .col(ColumnDef::new(ArticleTranslation::UpdatedAt).timestamp_with_time_zone().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk-article_translation-article_id")
                            .from(ArticleTranslation::Table, ArticleTranslation::ArticleId)
                            .to(Article::Table, Article::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(ArticleTranslation::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Article {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum ArticleTranslation {
    Table,
    Id,
    ArticleId,
    LanguageCode,
    Title,
    Content,
    Excerpt,
    MetaTitle,
    MetaDescription,
    CreatedAt,
    UpdatedAt,
}